﻿Imports Microsoft.Xna.Framework
Imports Runa.UI.Input
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.UI.Styles
Imports Runa.Resources.ResourceManager
Imports Runa.UI.Metadata
Imports Runa.Common.Helper

Namespace Global.Runa.UI.Controls

    Public Class SliderBar
        Inherits UIElementBase

        Private WithEvents Slider As Image
        Private m_mouseLeftButtonDown As Boolean = False
        Private m_lastPoint As Vector2 = Vector2.Zero

        <XamlAware(IsPlainType:=False, ElementName:="SliderTexture", Initializer:=GetType(Texture2DObjectConverter))>
        Public Property SliderTexture() As Texture2DObject
            Get
                Return Me.Slider.Foreground
            End Get
            Set(value As Texture2DObject)
                Me.Slider.Foreground = value
            End Set
        End Property

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
            Me.Slider = New Image(Me)
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase)
            MyBase.New(Name, Parent)
            Me.Slider = New Image(Me)
        End Sub

        Private Sub Slider_MouseLeftButtonDown(sender As Object, e As Input.MouseInputEventArgs) Handles Slider.MouseLeftButtonDown
            e.Handled = True
            Me.m_mouseLeftButtonDown = True
        End Sub

        Private Sub Slider_MouseLeftButtonUp(sender As Object, e As Input.MouseInputEventArgs) Handles Slider.MouseLeftButtonUp
            e.Handled = True
            Me.m_mouseLeftButtonDown = False
        End Sub

        Private Sub Slider_MouseMove(sender As Object, e As Input.MouseInputEventArgs) Handles Slider.MouseMove
            e.Handled = True
            If Me.m_mouseLeftButtonDown = True Then
                Dim locationToScreen = Me.Slider.Location + Me.Location
                Dim delta = Me.Location - locationToScreen
                Dim currentPoint = e.Location.ToVector2()
                Dim newX = Me.Slider.Location.X + (currentPoint - Me.m_lastPoint).X
                If newX >= 0 And newX < Me.Size.X - Me.Slider.Size.X Then
                    Me.Slider.Location = New Vector2(Me.Slider.Location.X + (currentPoint - Me.m_lastPoint).X, Me.Slider.Location.Y)
                End If
                Me.m_lastPoint = currentPoint
            End If
        End Sub

    End Class

End Namespace